<?php
declare(strict_types=1);

namespace App\Index;

use App\Controller;
use CodeItNow\BarcodeBundle\Utils\BarcodeGenerator;
use CodeItNow\BarcodeBundle\Utils\BarcodeType;
use CodeItNow\BarcodeBundle\Utils\QrCode;
use Helper\Ollama;
use Helper\ValidateCode;


// hook index_index_use.php

/**
 * @title    首页
 * @rank    3
 */
class Index extends Controller
{
    // hook index_index_start.php

    public bool $not_closed = true;

    /**
     * @title    导出
     * @rank    99
     */
    public function action_Exp()
    {
        $this->is_ajax=true;
        $config   = ['path' =>__WEBDIR__];
        $excel    = new \Vtiful\Kernel\Excel($config);
        // 导出测试文件
        $fileObject = $excel->fileName("tutorial01.xlsx", '测试店铺1');
        //$fileObject = $excel->constMemory("tutorial01.xlsx", '测试店铺1');
        //$fileOjbject->header(['name', 'age'])->data([['viest', 21]]);

        //$fileHandle = $fileObject->getHandle();
        //$format    = new \Vtiful\Kernel\Format($fileHandle);
        //$boldStyle = $format->bold()->toResource();
        //$numberStyle = $format->number('#,##0')->toResource();

        $data = [];
        for ($j = 0; $j < 5000; $j++) {
            $data[]=[
              "A{$j}",$j+1,$j+1,$j+1,$j+1,$j+1,$j+1,$j+1,$j+1
            ];
        }

        $data2 = [];
        for ($j = 0; $j < 200; $j++) {
            $data2[]=[
                "A{$j}",$j+1,$j+1,$j+1,$j+1,$j+1,$j+1,$j+1,$j+1
            ];
        }
        $fileObject->header(['name', 'age1', 'age2', 'age3', 'age4', 'age5', 'age6', 'age7', 'age8'])->data($data)->insertFormula(5, 2, '=SUM(B2:B10000)')->mergeCells('A1:E1', '我是一条虫')->setColumn('A:A', 40);
        for ($i = 2; $i <= 500; $i++) {
            $fileObject->addSheet("测试店铺{$i}")->header(['name', 'age1', 'age2', 'age3', 'age4', 'age5', 'age6', 'age7', 'age8'])->data($data2)->insertFormula(5, 2, '=SUM(B2:B10000)')->mergeCells('A1:E1', '我是一条虫')->setColumn('A:A', 40);
        }

        $filePath = $fileObject->output();
        $this->response('0000', ['ext' => 'xlsx', 'name' =>"tutorial01.xlsx", 'filename' => $filePath,'unlink'=>10], '导出成功', '', 1001);
    }
    /**
     * @title    首页
     * @rank    99
     */
    public function action_Index()
    {

        $keys = $this->StoreProduct->CacheKeys($this->StoreProduct->key_id . '*');
        $Products = !empty($keys) ? array_values($this->StoreProduct->CacheMGet($keys)) : [];
        array_walk($Products,function(&$v){
           $v['PkImg']='https://'._CONF('OssHost').$v['PkImg'];
        });
        $CateDataList = [];
        $CateDataList[] = $this->SysCate->GetListByCateID(1, 8);
        $CateDataList[] = $this->SysCate->GetListByCateID(2, 8);
        $CateDataList[] = $this->SysCate->GetListByCateID(3, 8);
        //典型店铺
        $caseShops=[
            '丽乐来旗舰店',
            '艺喜缘旗舰店',
            '怡馨菲旗舰店',
            'message家居旗舰店',
            '玻璃玫瑰旗舰店',
            '兰居舍旗舰店',
            '尖峰木作',
            '千为旗舰店',
            '索梵慕旗舰店'
        ];
        return $this->View(get_defined_vars());
    }



    /**
     * @title    二维码
     * @rank    99
     */
    public function action_Qrcode()
    {
        $color = $this->request->param('color', '');
        $url = $this->request->_S('SCHEME') . '://' . _CONF('site_host') . '/' . ltrim(urldecode($this->request->param('url', '')), '/');
        $qrCode = new QrCode();

        $color = ColorToRgb($color ? $color : '000000');
        $color['r'] = $color[0];
        $color['g'] = $color[1];
        $color['b'] = $color[2];
        $qrCode->setText($url)->setForegroundColor($color);
        $logo = __WEBDIR__ . 'logo.png';
        $qrCode->setText($url)->setLogo($logo)->setSize(300)->setPadding(5)->setErrorCorrection('high')->setImageType(QrCode::IMAGE_TYPE_WBMP);
        ob_start();
        $qrCode->render();
        $data = ob_get_contents();
        ob_clean();
        $this->response('0000', ['data' => $data], 'image/png', '', 1000);
    }

    /**
     * @title    二维码
     * @rank    99
     */
    public function action_Qrcodetxt()
    {

        $key = $this->request->param('key', 0);
        $text = $this->request->param('text', '');
        $color = $this->request->param('color', '');

        $url = $text ?: xn_decrypt(urldecode($this->request->param('txt', '')), $key);
        $this->CheckEmpty([$url, $color], ['缺少参数']);
        $qrCode = new QrCode();
        $logo = __WEBDIR__ . 'logo.png';
        $color = ColorToRgb($color ?: '000000');
        $color['r'] = $color[0];
        $color['g'] = $color[1];
        $color['b'] = $color[2];

        $qrCode->setText($url)->setForegroundColor($color);
        !$text && $qrCode->setLogo($logo);
        $text && $qrCode->setLabel($text)->setLabelFontSize(20);
        $qrCode->setSize(300)->setPadding(5)->setErrorCorrection('high')->setImageType(QrCode::IMAGE_TYPE_WBMP);
        ob_start();
        $qrCode->render();
        $data = ob_get_contents();
        ob_clean();
        $this->response('0000', ['data' => $data], 'image/png', '', 1000);
    }

    /**
     * @title    条形码
     * @rank    99
     */
    public function action_Barcode()
    {
        $num = $this->request->param('number', date('YmdHis'));
        $size = $this->request->param('size', 10);
        $color = $this->request->param('color', '');
        $height = $this->request->param('height', 15);

        $qrCode = new BarcodeGenerator();
        $qrCode->setFormat('png');
        $qrCode->setText($num);
        $qrCode->setType(BarcodeType::Code128);
        $qrCode->setScale(2);
        $color && $qrCode->setForegroundColor('#' . $color);
        $qrCode->setThickness($height);
        $qrCode->setFontSize($size);
        $data = $qrCode->generate();

        $this->response('0000', ['data' => $data], 'image/png', '', 1000);
    }


    //\Npl::GetWsChGeneral('【38狂欢】美宝莲纽约眼唇卸妆液脸部温和三合一卸妆水卸妆油正品 【38狂欢】美宝莲纽约眼唇卸妆液卸妆水三合一温和清洁卸妆油女 【国内现货】Maybelline/美宝莲眼部及唇部卸妆液套组70ml+40ml 日本kose高丝卸妆油液水女深层清洁温和眼唇脸部清爽保湿进口正品 UNNY旗舰店卸妆水官方正品敏感肌卸妆油女品牌眼唇卸妆液深层清洁');
//        $this->User->CacheSadd('users:1',1,2,3);
//        $this->User->CacheSadd('users:2',2,3,4);
//        \Swoole\Coroutine\System::exec("ffmpeg -ss 0 -t 30 -vf lavfi -i color=c=0x0000ff:s=800x1280:r=25 -vcodec libx264 -r:v 25   ".__WEBDIR__."test.mp4 -y");
//        \Swoole\Coroutine\System::exec("ffmpeg -ss 0 -t 20 -vf lavfi -i color=c=0x0000ff:s=800x1200:r=25 -vcodec libx264 -r:v 25   ".__WEBDIR__."test.mp4 -y");
//        $str = 'ffmpeg -i '.__WEBDIR__.'test.mp4 -vf drawtext=fontcolor=black:fontsize=50:text=\'Hello World\':x=0:y=100 -vf drawtext=fontcolor=red:fontsize=50:text=\'Hello World\':x=100:y=100 -y '.__WEBDIR__."out.mp4";
//        \Swoole\Coroutine\System::exec($str);

    //var_dump(\Swoole\Coroutine\System::exec("top"));
//            !is_dir( __WEBDIR__."1/tmp") && mkdir( __WEBDIR__."1/tmp",0777,1);
//
//            echo 'gamestart';
//            //var_dump(\Swoole\Coroutine\System::exec("ffmpeg -formats"));
//            \Swoole\Coroutine\System::exec("ffmpeg -loglevel quiet -i ". __WEBDIR__."1/0001_01.mp4 -y -f image2 -frames 1  ". __WEBDIR__."1/a.jpg");
//            \Swoole\Coroutine\System::exec("ffmpeg -loglevel quiet -i ". __WEBDIR__."1/0001_01.mp4 -r 24 ". __WEBDIR__."1/tmp/image-%3d.jpg");
//            \Swoole\Coroutine\System::exec("ffmpeg -loglevel quiet -f image2 -i ". __WEBDIR__."1/tmp/image-%3d.jpg -vcodec libx264 -r 24  ". __WEBDIR__."1/tt.mp4 -y");
//            rmdir_recusive(__WEBDIR__."1/tmp",1);
//            echo 'gameover';

    //var_dump(\Swoole\Coroutine\System::exec("ffmpeg -formats"));


    // hook index_index_end.php

}

?>